package socketDemo1;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class SocketDemo1 {
    public static void main(String[] args) throws IOException {
        //1.新建一个发送对象
        DatagramSocket dp=new DatagramSocket();
        //2.数据打包
        //需要:1.数据(byte) 2.数据长度 3.地址 4.端口号
        System.out.println("欢迎使用小唐聊天软件");
        System.out.println("================");
        System.out.println("按回车发送消息,退出请输入'--'");
        String str=" ";
        Scanner sc=new Scanner(System.in);
        while(!str.equals("--")){
            str=sc.nextLine();
            byte[] strBytes = str.getBytes();
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port=10086;
            DatagramPacket packet = new DatagramPacket(strBytes,strBytes.length,address,port);
            //3.数据发送
            dp.send(packet);
            System.out.println("message is sent");
        }
        //4.关闭
        dp.close();
    }
}
